import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Calendar;


public class Data extends Thread{
	
	static String driver,url,id,pw;
	static Connection conn;
	static Statement stmt;
	String sql;
	static ResultSet rs;
	int cnt;
	static {

		driver = "oracle.jdbc.driver.OracleDriver";
		url = "jdbc:oracle:thin:@localhost:1521:XE";
		id = "cristalriver";
		pw = "tt4ever";
		
		try {
			Class.forName(driver);
			conn = DriverManager.getConnection(url, id, pw);
			stmt = conn.createStatement();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		
		
	}
	
	public void checkMsg(){
		//System.out.println("check");
		String date = null;
		if(LoginInfo.getId()!=null && LoginInfo.getIdu()!=null){
		sql =String.format("select msg,sdate from talk where send = '0' and idm = '%s' and idu = '%s'",LoginInfo.getIdu(),LoginInfo.getId());
		try {
			//System.out.println(sql);
			//System.out.println("getData");
			rs = stmt.executeQuery(sql);
			
		//String k = ChatRoom.msgs.getText();
		//String z = "";
		  	
			while(rs.next()){
				ChatRoom.msgs.setText(ChatRoom.msgs.getText()+LoginInfo.getIdu()+" : "+rs.getString("msg")+"\n");
				//z = rs.getString("msg");
				date = rs.getString("sdate");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
		}
		
		
		sql =String.format("update talk set send = '1' where send = '0' and idm = '%s' and idu = '%s' and "
				+ " sdate = '%s' ",LoginInfo.getIdu(),LoginInfo.getId(),date);
		
		
		try {
			cnt = stmt.executeUpdate(sql);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		}
	}
	
	public void sendMsg(){
		if(LoginInfo.send==1){
		
		sql =String.format("insert into talk values('%s','%s','%s','%s','0')",LoginInfo.getId(),LoginInfo.getIdu(),LoginInfo.tsend,nowTime());
		//System.out.println(sql);
		try {
			cnt = stmt.executeUpdate(sql);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		 LoginInfo.tsend = "";
		 LoginInfo.send=0;
		}
	}
	
	public String nowTime(){
		Calendar now = Calendar.getInstance();
		//now.getTime();
		//System.out.println(now.getTime());
		
		int y=now.get(Calendar.YEAR);
		int m=now.get(Calendar.MONTH)+1;
		int d=now.get(Calendar.DATE);
		int h=now.get(Calendar.HOUR_OF_DAY);
		int mm=now.get(Calendar.MINUTE);
		int s=now.get(Calendar.SECOND);
		return String.format("%s/%s/%s/%s/%s/%s",checkDate(y),checkDate(m),checkDate(d),checkDate(h),checkDate(mm),checkDate(s));
		
	}
	 String checkDate(int d){
		String result;
		if(d<10){
			result ="0"+d;
		}else
			result = String.valueOf(d);
		return result;
	}
	
	
	
	
	@Override
	public void run() {
		
		// TODO Auto-generated method stub
		while(true){
			//System.out.println("1");
			checkMsg();
			sendMsg();
			
		}
		
	}
	}
